package com.common.baselibrary.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.common.baselibrary.TimeActivityUyils;
import com.common.baselibrary.progressutil.KProgressHUD;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment extends Fragment {

    public Context mContext;
    private KProgressHUD hud;
    private Unbinder unbinder;

    protected float mDensity;
    protected int mDensityDpi;
    protected int mWidth;
    protected int mHeight;

    protected float mRatio;

    protected int mAvatarSize;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        JMessageClient.registerEventReceiver(this);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mDensity = dm.density;
        mDensityDpi = dm.densityDpi;
        mWidth = dm.widthPixels;
        mHeight = dm.heightPixels;
        mRatio = Math.min((float) mWidth / 720, (float) mHeight / 1280);
        mAvatarSize = (int) (50 * mDensity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        mContext = getActivity();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        initProgress();
        clickJussage();
//        initReceiver();
    }

    public boolean clickJussage() {
        return TimeActivityUyils.isFastClick();
    }


    public abstract int getLayoutId();

    public abstract void init(View view);

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) unbinder.unbind();
    }

    private void initProgress() {
        hud = KProgressHUD.create(mContext);
    }

    public void showFragmentTextProgress() {
        if (hud == null) {
            hud = KProgressHUD.create(mContext);
        }
        if (!hud.isShowing()) {
            hud.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setDimAmount(0.5f)
                    .setLabel("正在加载...")
                    .show();
            scheduleDismiss();
        }
    }

    public void showFragmentProgress(String content) {
        if (hud == null) {
            hud = KProgressHUD.create(mContext);
        }
        if (!hud.isShowing()) {
            hud.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setDimAmount(0.5f)
                    .setLabel(content)
                    .show();
            scheduleDismiss();
        }
    }

    public void closeFragmentProgress() {
//        hud.dismiss();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (hud != null && hud.isShowing()) {
                    hud.dismiss();
                }
            }
        }, 200);
    }

    private void scheduleDismiss() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hud.dismiss();
            }
        }, 15000);
    }

    private Handler handler = new Handler();

    @Override
    public void onResume() {
        super.onResume();
    }

    protected void toast(String s) {
        Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}
